<?php

/**
 * This is the model class for table "gpsdatainfo_tb".
 *
 * The followings are the available columns in table 'gpsdatainfo_tb':
 * @property integer $GPSDataInfoId
 * @property string $vechicleId
 * @property string $time
 * @property string $longitude
 * @property string $latitude
 * @property string $rate
 * @property string $direction
 * @property string $height
 * @property string $mileage
 * @property string $stateInfo
 * @property integer $directNotChage
 */
class GPSData extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return GPSData the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'gpsdatainfo_tb';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('directNotChage', 'numerical', 'integerOnly'=>true),
			array('vechicleId, longitude, latitude, rate, direction, height, mileage', 'length', 'max'=>50),
			array('stateInfo', 'length', 'max'=>100),
			array('time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('GPSDataInfoId, vechicleId, time, longitude, latitude, rate, direction, height, mileage, stateInfo, directNotChage', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'GPSDataInfoId' => 'Gpsdata Info',
			'vechicleId' => 'Vechicle',
			'time' => 'Time',
			'longitude' => 'Longitude',
			'latitude' => 'Latitude',
			'rate' => 'Rate',
			'direction' => 'Direction',
			'height' => 'Height',
			'mileage' => 'Mileage',
			'stateInfo' => 'State Info',
			'directNotChage' => 'Direct Not Chage',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('GPSDataInfoId',$this->GPSDataInfoId);
		$criteria->compare('vechicleId',$this->vechicleId,true);
		$criteria->compare('time',$this->time,true);
		$criteria->compare('longitude',$this->longitude,true);
		$criteria->compare('latitude',$this->latitude,true);
		$criteria->compare('rate',$this->rate,true);
		$criteria->compare('direction',$this->direction,true);
		$criteria->compare('height',$this->height,true);
		$criteria->compare('mileage',$this->mileage,true);
		$criteria->compare('stateInfo',$this->stateInfo,true);
		$criteria->compare('directNotChage',$this->directNotChage);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function getCarsByVechicleIdAndTimes($vechicleId,$timeBegin,$timeEnd)
	{
	 
	 $condition = 'time between :timeBegin and :timeEnd and vechicleId = :vechicleId';
	 $params=array(':vechicleId'=>$vechicleId,':timeBegin'=>$timeBegin
	               ,':timeEnd'=>$timeEnd);
     return $this->model()->findAll($condition,$params);
	}
	public function getCarsBylimit($vechicleId,$pageIndex,$pageSize){
		 $criteria = new CDbCriteria;
		 $criteria->condition = 'vechicleId = :vechicleId';
		 $criteria->params = array(':vechicleId'=>$vechicleId);
	     $criteria->order = 'time asc';
	     $count=$this->model()->count($criteria);
	     $pages=new CPagination($count);
	     $pages->setCurrentPage($pageIndex);
	     $pages->pageSize=$pageSize;
	     $pages->applyLimit($criteria);
	     return $this->model()->findAll($criteria);
		
	}
	public function getCarsByTime($timeBegin,$timeEnd)
	{
	 $condition = 'time between :timeBegin and :timeEnd';
	 $params=array(':timeBegin'=>$timeBegin,':timeEnd'=>$timeEnd);
     return $this->model()->findAll($condition,$params);
	}
	
	
	
	
	
	

	
	
}